package com.atguigu.day07;

import com.atguigu.utils.ClickEvent;
import com.atguigu.utils.ClickSource;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

// select * from table group by username, url;
public class Example7 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        env
                .addSource(new ClickSource())
                .map(new MapFunction<ClickEvent, Tuple2<Tuple2<String, String>, Long>>() {
                    @Override
                    public Tuple2<Tuple2<String, String>, Long> map(ClickEvent value) throws Exception {
                        return Tuple2.of(
                                Tuple2.of(value.username, value.url),
                                value.ts
                        );
                    }
                })
                .keyBy(new KeySelector<Tuple2<Tuple2<String, String>, Long>, Tuple2<String, String>>() {
                    @Override
                    public Tuple2<String, String> getKey(Tuple2<Tuple2<String, String>, Long> value) throws Exception {
                        return value.f0;
                    }
                })
                .reduce(new ReduceFunction<Tuple2<Tuple2<String, String>, Long>>() {
                    @Override
                    public Tuple2<Tuple2<String, String>, Long> reduce(Tuple2<Tuple2<String, String>, Long> value1, Tuple2<Tuple2<String, String>, Long> value2) throws Exception {
                        return value1;
                    }
                })
                .print();

        env.execute();
    }
}
